import {
  MiddlewareConsumer,
  Module,
  NestModule,
  RequestMethod,
} from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserModule } from './user/user.module';
import { EventsGateway } from './gateWay/events.gateway';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TagsModule } from './tags/tags.module';
import { ArticleModule } from './article/article.module';
import { RoleModule } from './role/role.module';
import { AuthMiddleware } from './middeware/auth.middleware';
import { UserController } from './user/user.controller';
import { AuthGuard } from './services/auth.guard';
import { APP_GUARD } from '@nestjs/core';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root1234',
      database: 'myblog',
      entities: [__dirname + '/database/*'],
      synchronize: true,
    }),
    UserModule,
    TagsModule,
    ArticleModule,
    RoleModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    EventsGateway, // 全局角色管道
    {
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    // consumer
    //   .apply(AuthMiddleware)
    //   .exclude(
    //     {
    //       path: '/user/login',
    //       method: RequestMethod.POST,
    //     },
    //     {
    //       path: '/user/code',
    //       method: RequestMethod.GET,
    //     },
    //   )
    //   .forRoutes(UserController);
  }
}
